package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.AgentRoleGroupIdDto;
import com.frejoys.common.dto.agent.AgentUserSaveDto;
import com.frejoys.common.dto.agent.AgentUserUpdateDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.agent.AgentUserInfoVo;
import com.frejoys.common.vo.agent.AgentUserVo;
import com.frejoys.service.AgentRoleGroupService;
import com.frejoys.service.AgentUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "代理角色组关系", description = "代理角色组关系")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminAgentUserController {

    private final AgentUserService agentUserService;

    private final AgentRoleGroupService agentRoleGroupService;


    /**
     * 设置代理用户到代理用户组
     */
    @Operation(summary = "设置代理用户到代理用户组")
    @PostMapping("/agent/user/save")
    public Result saveAgentUser(@RequestBody @Validated AgentUserSaveDto dto) {
        agentUserService.saveAgentUser(dto);
        return Result.success();
    }

    /**
     * 修改代理用户到代理用户组
     */
    @Operation(summary = "修改代理用户到代理用户组")
    @PostMapping("/agent/user/update")
    public Result updateAgentUser(@RequestBody @Validated AgentUserUpdateDto dto) {
        agentUserService.updateAgentUser(dto);
        return Result.success();

    }

    /**
     * 查询自己推广的用户
     */
    @Operation(summary = "查询自己推广的用户")
    @PostMapping("/agent/user/listSelf")
    public Result<List<AgentUserInfoVo>> listAgentUserById(){
        return Result.success(agentRoleGroupService.listAgentUserById(RequestUtil.userId()));
    }


    @Operation(summary = "删除代理用户组的代理用户")
    @PostMapping("/agent/user/delete")
    public Result deleteSaveAgentUser(@RequestBody @Validated IdDto dto) {
        agentUserService.deleteSaveAgentUser(dto);
        return Result.success();
    }

    @Operation(summary = "查询职员列表")
    @PostMapping("/agent/user/agentListInfo")
    public Result<IPage<AgentUserVo>> agentListInfo(@RequestBody @Validated AgentRoleGroupIdDto dto) {
        return Result.success(agentUserService.agentListInfo(dto));
    }
}
